package dongze.com.ckdj.adapter;

import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

import dongze.com.ckdj.R;
import dongze.com.ckdj.data.entity.response.QueryMassLossRecordsResponse;
import dongze.com.ckdj.databinding.MassLossRecordListItemButtonBinding;
import dongze.com.ckdj.databinding.model.MassLossRecordListItemModel;
import dongze.com.ckdj.fragment.ProcessableFragment;
import dongze.com.ckdj.map.MapUtil;

/**
 * Created by fanqwz on 2017/7/26.
 */

public class MassLossRecordListAdapter extends BaseAdapter {
    final List<QueryMassLossRecordsResponse> data;
    private MapUtil mapUtil;
    private ProcessableFragment fragment;
    private OnEditListener listener;

    public MassLossRecordListAdapter(ProcessableFragment fragment, MapUtil mapUtil, List<QueryMassLossRecordsResponse> data, OnEditListener listener) {
        this.mapUtil = mapUtil;
        this.data = data;
        this.fragment = fragment;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data == null ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            MassLossRecordListItemButtonBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.mass_loss_record_list_item_button, parent, false);
            binding.setModel(new MassLossRecordListItemModel());
            convertView = binding.getRoot();
            convertView.setTag(binding.getModel());
        }

        final QueryMassLossRecordsResponse response = data.get(position);
        MassLossRecordListItemModel model = (MassLossRecordListItemModel) convertView.getTag();
        model.reason.set(response.getLoss_reason());
        model.date.set(response.getcTime());
        model.level.set(response.getLoss_level());
        model.place.set(response.getLoss_place());
        model.user.set(response.getcUser());
//        ((SwipeToDeleteLayout) convertView).setState(SwipeToDeleteLayout.State.CLOSE);
        model.onDelete.set(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data.remove(response);
                notifyDataSetChanged();
                listener.onDelete(response);

            }
        });
        model.onEdit.set(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onEdit(response);
            }
        });
        return convertView;
    }

    public interface OnEditListener {
        void onDelete(QueryMassLossRecordsResponse id);
        void onEdit(QueryMassLossRecordsResponse id);
    }
}
